#include "stdafx.h"
#include "XColorDrawImpl.h"

#include "DrawUtil.h"
#include "XCodeDefine.h"
#include "IntSkinImpl.h"
#include "Colorize.h"

IMPLEMENT_SKIN_CLASS(CXColorDrawImpl);

CXColorDrawImpl::CXColorDrawImpl()
{
    m_dwFlags    = ColorFlag_Horz;
    m_clrStart   = RGB(255, 255, 255);
    m_clrEnd     = RGB(255, 255, 255);

    m_hue = 0;
    m_sat = 0;
    m_light = 0;
}

CXColorDrawImpl::~CXColorDrawImpl()
{
    ;
}

//////////////////////////////////////////////////////////////////////////
// IXColorDraw
void CXColorDrawImpl::put_Color(IN COLORREF clr)
{
    m_dwFlags = IntSkinImpl::ModifyFlags(m_dwFlags, ColorFlag_Gradient, FALSE);
    m_dwFlags = IntSkinImpl::ModifyFlags(m_dwFlags, ColorFlag_Pure, TRUE);
    m_clrStart = clr;
}

void CXColorDrawImpl::get_Color(OUT LPCOLORREF pclr)
{
    *pclr = m_clrStart;
}


void CXColorDrawImpl::put_GradientColor(IN COLORREF clrStart, IN COLORREF clrEnd)
{
    m_dwFlags = IntSkinImpl::ModifyFlags(m_dwFlags, ColorFlag_Pure, FALSE);
    m_dwFlags = IntSkinImpl::ModifyFlags(m_dwFlags, ColorFlag_Gradient, TRUE);

    m_clrStart = clrStart;
    m_clrEnd = clrEnd;
}

void CXColorDrawImpl::get_GradientColor(OUT LPCOLORREF pclrStart, OUT LPCOLORREF pclrEnd)
{
    *pclrStart = m_clrStart;
    *pclrEnd = m_clrEnd;
}

void CXColorDrawImpl::put_Direction(IN BOOL bHorz)
{
    m_dwFlags = IntSkinImpl::ModifyFlags(m_dwFlags, ColorFlag_Vert, !bHorz);
    m_dwFlags = IntSkinImpl::ModifyFlags(m_dwFlags, ColorFlag_Horz, bHorz);
}

void CXColorDrawImpl::get_Direction(OUT LPBOOL pbHorz)
{
    *pbHorz = IntSkinImpl::CheckFlags(m_dwFlags, ColorFlag_Horz);
}

//////////////////////////////////////////////////////////////////////////
// IXDraw
HRESULT CXColorDrawImpl::Draw(IN XCANVAS xCanvas, const RECT* lpDes)
{
    COLORREF clrStart = m_clrStart;
    COLORREF clrEnd = m_clrEnd;

    if(IntSkinImpl::CheckFlags(m_dwFlags, ColorFlag_Colorize))
    {
        clrStart = Colorize::AdjustColorPixel(m_clrStart, m_hue, m_sat, m_light);
        clrEnd = Colorize::AdjustColorPixel(m_clrEnd, m_hue, m_sat, m_light);
    }

    if(IntSkinImpl::CheckFlags(m_dwFlags, ColorFlag_Gradient))
    {
        BOOL bHorz = IntSkinImpl::CheckFlags(m_dwFlags, ColorFlag_Horz);
        GRADIENT_RECT grdRect = { 0, 1 };
        TRIVERTEX triVertext[ 2 ] =
        {
            lpDes->left,
            lpDes->top,
            GetRValue( clrStart ) << 8,
            GetGValue( clrStart ) << 8,
            GetBValue( clrStart ) << 8,
            0x0000,			
            lpDes->right,
            lpDes->bottom,
            GetRValue( m_clrEnd ) << 8,
            GetGValue( m_clrEnd ) << 8,
            GetBValue( m_clrEnd ) << 8,
            0x0000
        };
        ::GradientFill((HDC)xCanvas, triVertext, 2, &grdRect, 1, bHorz ? GRADIENT_FILL_RECT_H : GRADIENT_FILL_RECT_V);
    }
    else
    {
        DrawUtil::FillRect(xCanvas, lpDes, clrStart);
    }
    return S_OK;
}

HRESULT CXColorDrawImpl::Draw(IN XCANVAS xCanvas, IN const RECT* lpSrc, IN const RECT* lpDes)
{
    return Draw(xCanvas, lpDes);
}

BOOL CXColorDrawImpl::IsDrawValid()
{
    return TRUE;
}

void CXColorDrawImpl::EnableColorize(IN BOOL bEnable)
{
    m_dwFlags = IntSkinImpl::ModifyFlags(m_dwFlags, ColorFlag_Colorize, bEnable);
}

void CXColorDrawImpl::Colorize(IN int hue, IN int sat, IN int light)
{
    EnableColorize(TRUE);

    m_hue = hue;
    m_sat = sat;
    m_light = light;
}
